import urllib
import json
import ast
from google.appengine.api import urlfetch


def create_queue_by_id(admin_id, queue_name, queue_id, queue_type=0, queue_img=None):
   
    url = 'http://localhost:8080/api/' + admin_id + '/queues'
   
    recieved_params = {'queue_name': queue_name, 'queue_id': queue_id,
                       'queue_type': str(queue_type), 'queue_img': queue_img}
     
    params = json.dumps(recieved_params)
   
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'})
   
    if ((result.status_code == 201) or (result.status_code == 200)):
        received_data = result.content
        return int(received_data)

   
def stop_queue(queue_id):  
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-status"
   
    params = urllib.urlencode({'status': 1})
   
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.PUT,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'})
   
    if (result.status_code == 200):
        return


def delete_queue(queue_id):
    url = 'http://localhost:8080/api/queue/' + queue_id
   
    result = urlfetch.fetch(url=url,
                            method=urlfetch.DELETE)
   
    if (result.status_code == 204):
        return
       
       
def resume_queue(queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-status"
   
    params = urllib.urlencode({'status': 0})
   
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.PUT,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'})
   
    if (result.status_code == 200):
        return


def get_queue_image(queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-image"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        return result.content

 
def set_queue_image(queue_id, queue_image):  
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-image"
   
    params = urllib.urlencode({'queue_image': queue_image})
   
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.PUT,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'})
   
    if (result.status_code == 200):
        return


def del_queue_image(queue_id):  
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-image"
   
    result = urlfetch.fetch(url=url,
                            method=urlfetch.DELETE)
   
    if (result.status_code == 204):
        return

   
def advance_queue(queue_id, ext=None):
   
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-advance"
   
    if ext is None:
        ext = ""
   
    params = urllib.urlencode({'ext': ext})
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'})
   
    if (result.status_code == 200):
        return result.content


def get_queue_type(queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-info"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data["queueType"]


def get_inservice_users(queue_id, user_id=None):  
    if user_id is None:
        url = "http://localhost:8080/api/queue/" + queue_id + "/users/in-service-users"
    else:
        url = "http://localhost:8080/api/queue/" + queue_id + "/users/in-service-users?user_id=" + user_id
       
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data
 
 
def get_next_users(queue_id, count):
    url = "http://localhost:8080/api/queue/" + queue_id + "/users?amount=" + str(count)
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data
 
   
def get_queue_size(queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-info"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data["queueSize"]


def get_queue_name(queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-info"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data["queueName"]

 
def get_queue_status(queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/queue-status"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        return int(result.content)


def get_all_users_in_queue(queue_id):
    url = 'http://localhost:8080/api/queue/' + queue_id
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        return result.content


def get_user_details(user_id):
    url = "http://localhost:8080/api/users/" + user_id
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data


def create_user(details):
    url = 'http://localhost:8080/api/users'
   
    params = json.dumps(details)
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/json'})
   
    if (result.status_code == 201):
        return result.content


def join_queue(details, queue_id):
    url = 'http://localhost:8080/api/queue/' + queue_id
   
    params = json.dumps(details)
    result = urlfetch.fetch(url=url,
                            payload=params,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/json'})
   
    if (result.status_code == 201):
        return result.content
   
   
def get_position_status(position):
    url = "http://localhost:8080/api/position/" + position
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data["positionStatus"]


def leave_queue(user_id, queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/users/" + user_id
   
    result = urlfetch.fetch(url=url,
                            method=urlfetch.DELETE)
   
    if (result.status_code == 204):
        return
 
     
def get_pos_number(position):
    url = "http://localhost:8080/api/position/" + position
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data["positionNumber"]
   
 
def get_user_pos_in_queue(user_id, queue_id):
    url = "http://localhost:8080/api/queue/" + queue_id + "/users/" + user_id
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        position = result.content
        return position
 
   
def get_queue_by_position(position):
    url = "http://localhost:8080/api/position/" + position
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data["queueId"] 


def get_user_positions(user_id):
    url = 'http://localhost:8080/api/users/' + user_id + '/user-positions'
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        return result.content
   
 
def get_all_queues_from_admin_by_id(admin_id):
    url = "http://localhost:8080/api/" + admin_id + "/queues"
   
    result = urlfetch.fetch(url)  
   
    if (result.status_code == 200):
        received_data = json.loads(result.content)
        return received_data


def is_valid_user(user_id):
    url = "http://localhost:8080/api/user-check" + "?" + "user_id=" + user_id
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        data = result.content
        b = ast.literal_eval(data)
        return b


def is_valid_queue(queue_id):
    url = "http://localhost:8080/api/queue-check" + "?" + "queue_id=" + queue_id
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        data = result.content
        b = ast.literal_eval(data)
        return b 
 

def get_queue_statistics(queue_id):
    url = 'http://localhost:8080/api/queue/' + queue_id + "/statistics"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        data = json.loads(result.content)
        last_avg = data["last_avg"]
        total_avg = data["total_avg"]
        min_delta = data["min_delta"]
        max_delta = data["max_delta"]
        return (last_avg, total_avg, min_delta, max_delta)


def get_last_service_delta(queue_id):
    url = 'http://localhost:8080/api/queue/' + queue_id + "/last-service-delta"
   
    result = urlfetch.fetch(url)
   
    if (result.status_code == 200):
        data = result.content
        return float(data)